import { Controller, Get, Post, Body, Patch, Param, Delete, Inject } from '@nestjs/common';
import { RoleService } from './role.service';
import { AssignRoleDto, CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';

@Controller('admin/role')
@ApiTags('角色管理')
export class RoleController {

  @Inject()
  private readonly roleService: RoleService;

  //创建角色
  @ApiOperation({
    summary: '创建角色',
  })
  @Post()
  async create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  // 查询所有角色
  @ApiOperation({
    summary: '查询所有角色',
  })
  @Get()
  async findAll() {
    return this.roleService.findAll();
  }

  // 分配角色
  @ApiOperation({
    summary: '分配角色',
  })
  @Post('assignRole')
  async assignRole(@Body() body: AssignRoleDto) {
    return this.roleService.assignRole(body.userId, body.roleId);
  }

  // 修改用户角色
  @ApiOperation({
    summary: '修改用户角色',
  })
  @Patch(':id')
  async update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(id, updateRoleDto);
  }

  // 查看某个角色下的用户
  @ApiOperation({
    summary: '查看某个角色下的用户',
  })
  @Get('findUserByRole/:roleId')
  async findUserByRole(@Param('roleId') roleId: string) {
    return this.roleService.findUserByRole(roleId);
  }

  // 查看某个角色下的权限
  @ApiOperation({
    summary: '查看某个角色下的权限',
  })
  @Get('findAuthorityByRole/:roleId')
  async findAuthorityByRole(@Param('roleId') roleId: string) {
    return this.roleService.findAuthorityByRole(roleId);
  }
}
